package servlet;

import dao.TuitionRecordDao;
import vo.TuitionRecord;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDate;

@WebServlet("/update_tuition_record")
public class update_tuitionrecord extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");

        PrintWriter out = response.getWriter();

        TuitionRecordDao tuitionRecordDao = new TuitionRecordDao();

        String studentId = request.getParameter("student_id");
        String studentName = request.getParameter("student_name");
        LocalDate paymentDate = LocalDate.parse(request.getParameter("payment_date"));
        double amount = Double.parseDouble(request.getParameter("amount"));

        TuitionRecord tuitionRecord = new TuitionRecord(studentId, studentName, paymentDate, amount);

        try {
            boolean updated = tuitionRecordDao.updateTuitionRecord(tuitionRecord);
            if (updated) {
                response.sendRedirect("one_page_tuition_records");
            } else {
                out.println("Failed to update record.");
            }
        } catch (Exception e) {
            out.print(e);
        }
    }
}
